package com.ricchieri.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.dao.interf.IUserDao;
import com.ricchieri.filter.UserFilter;
import com.ricchieri.model.User;
import com.ricchieri.util.StringUtils;

public class UserDao extends GenericDao implements IUserDao {

	private static final long serialVersionUID = 5707688526647264633L;

	@Override
	@SuppressWarnings("unchecked")
	public List<User> getAllUsers(UserFilter user) {
		
		Criteria criteria = criteriaUsers(user);
		return criteria.list();
	}
	
	@Override
	public Integer getAllUserCountByFilter(UserFilter user) {
		Criteria criteria = criteriaUsers(user);
		Long result = (Long)criteria.setProjection(Projections.rowCount()).uniqueResult();
		return new Integer(result.intValue());
	}
		
	
	@Override
	@SuppressWarnings("unchecked")
	public User getUserByFilter(UserFilter user) {
		Criteria criteria = criteriaUsers(user);
		List<User> users = criteria.list();
		if (users == null || users.isEmpty()) {
			return null;
		} else {
			return users.get(0);
		}
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<User> getAllUserByFilter(UserFilter user) {
		Criteria criteria = criteriaUsers(user);
		return criteria.list();
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<User> getAllUserPaginatedByFilter(UserFilter user, int page) {
		
		Criteria criteria = criteriaUsers(user);
		criteria.setMaxResults(BusinessSettings.ROW_MAX_RESULT);
		criteria.setFirstResult(page);
		
		return criteria.list();
	}

	private Criteria criteriaUsers(UserFilter user) {
		Criteria criteria = super.criteriaAllBasic(user, User.class);
		
		if (!StringUtils.isEmpty(user.getUsername())) {
			criteria.add(Restrictions.eq(getTableName().concat(".username"), user.getUsername()));
		}
		
		if (!StringUtils.isEmpty(user.getFirstName())) {
			criteria.add(Restrictions.like(getTableName().concat(".firstName"), "%".concat(user.getFirstName()).concat("%")));
		}
		
		if (!StringUtils.isEmpty(user.getLastName())) {
			criteria.add(Restrictions.like(getTableName().concat(".lastName"), "%".concat(user.getLastName()).concat("%")));
		}
		
		if (!StringUtils.isEmpty(user.getDni())) {
			criteria.add(Restrictions.eq(getTableName().concat(".dni"), user.getDni()));
		}
		
		if (!StringUtils.isEmpty(user.getDocumentType())) {
			criteria.createAlias(getTableName().concat(".documentType"), "documentType");
			criteria.add(Restrictions.eq("documentType.id", Integer.parseInt(user.getDocumentType())));
		}
		
		if (!StringUtils.isEmpty(user.getEmail())) {
			criteria.createAlias(getTableName().concat(".userEmail"), "userMail");
			criteria.add(Restrictions.like("userMail.email", "%".concat(user.getEmail()).concat("%")));
		}
		
		if (!StringUtils.isEmpty(user.getRole())) {
			criteria.createAlias(getTableName().concat(".roles"), "userRole");
			criteria.createAlias("userRole.role", "role");
			criteria.add(Restrictions.like("role.description", "%".concat(user.getRole()).concat("%")));
		}
		
		
		if (!StringUtils.isEmpty(user.getPhoneNumber())) {
			criteria.createAlias(getTableName().concat(".userPhone"), "userPho");
			criteria.add(Restrictions.eq("userPho.number", user.getPhoneNumber()));
		}
		
		return criteria;
	}
	
	@Override
	public User getUserById(UserFilter userFilter){
		Criteria criteria = criteriaUsers(userFilter);
		return (User) criteria.list().get(0);
	}

	@Override
	protected String getTableName() {
		return "user";
	}
}
